﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using HgCo.WindowsLive.SkyDrive;
using Microsoft.HomeServer.Extensibility;
using Microsoft.HomeServer.SDK.Interop.v1;

namespace HomeServerConsoleTab.SkyDrive
{
    public class HomeServerTabExtender : Microsoft.HomeServer.Extensibility.IConsoleTab 
    {
        private IConsoleServices services;
        private SkyDrive nPanel;
        private WHSInfoClass whsInfo;

        public HomeServerTabExtender(int width, int height, IConsoleServices consoleServices)
        {
            nPanel = new SkyDrive();
            nPanel.Size = new Size(width, height);
            this.services = consoleServices;
            whsInfo = new WHSInfoClass();
            nPanel.btnSave.Click += new EventHandler(btnSave_Click);

            GetHomeServerInfo();
        }

        private void GetHomeServerInfo()
        {
            List<AppFolder> _appFolders = new List<AppFolder>();
            List<CliComputers> _cliComputerses = new List<CliComputers>();
            List<ShaFolder> _shaFolders = new List<ShaFolder>();

            #region Application Folders

            foreach (IApplicationFolder _applicationFolder in whsInfo.GetApplicationFolders())
            {
                _appFolders.Add(new AppFolder
                {
                    FriendlyName = _applicationFolder.FriendlyName,
                    IsDuplicated = _applicationFolder.IsDuplicated,
                    Name = _applicationFolder.Name,
                    Path = _applicationFolder.Path
                });
            }

            nPanel.cbApplicationFolders.DataSource = _appFolders;
            nPanel.cbApplicationFolders.ValueMember = "Path";
            nPanel.cbApplicationFolders.DisplayMember = "FriendlyName";
            nPanel.cbApplicationFolders.Update();

            #endregion

            #region Share Info

            foreach (IShareInfo _sharedFolder in whsInfo.GetShareInfo())
            {
                _shaFolders.Add(new ShaFolder
                {
                    Name = _sharedFolder.Name,
                    Path = _sharedFolder.Path
                });
            }

            nPanel.cbSharedFolders.DataSource = _shaFolders;
            nPanel.cbSharedFolders.ValueMember = "Path";
            nPanel.cbSharedFolders.DisplayMember = "Name";
            nPanel.cbSharedFolders.Update();

            #endregion

            #region Computer Info

            foreach (IComputerInfo _computerInfo in whsInfo.GetClientComputerInfo())
            {
                _cliComputerses.Add(new CliComputers()
                {
                    ComputerName = _computerInfo.ComputerName
                });
            }

            nPanel.cbSharedFolders.DataSource = _cliComputerses;
            nPanel.cbSharedFolders.DisplayMember = "ComputerName";
            nPanel.cbSharedFolders.Update();

            #endregion
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            GetSkyDriveInfo();
        }

        private void GetSkyDriveInfo()
        {
            HgCo.WindowsLive.SkyDrive.SkyDriveWebClient _client = new SkyDriveWebClient();
            //_client.LogOn(nPanel.txtAccountName.Text, nPanel.txtPassword.Text);
            _client.LogOn("whsbackup@hotmail.com", "qazxsw");

            WebDriveInfo _info = _client.GetWebDriveInfo();

            nPanel.lblAccountName.Text = "whsbackup@hotmail.com";
            nPanel.lblCID.Text = _info.Cid;
            nPanel.lblFreeDiskSpace.Text = _info.FreeDiskSpace;
            nPanel.lblUsedDiskSpace.Text = _info.UsedDiskSpace;

            nPanel.cSkyDrive.Series["DiskSpace"].Points.AddXY("Used Space", decimal.Parse(_info.UsedDiskSpace.Replace(" GB", "").Replace(" MB", "")));
            nPanel.cSkyDrive.Series["DiskSpace"].Points.AddXY("Free Space", decimal.Parse(_info.FreeDiskSpace.Replace(" GB", "").Replace(" MB", "")));
            nPanel.cSkyDrive.Series["DiskSpace"].IsValueShownAsLabel = true;
            nPanel.cSkyDrive.Series["DiskSpace"]["LabelStyle"] = "Center";

        }

        private void Upload()
        {
            //HgCo.WindowsLive.SkyDrive.SkyDriveWebClient _client = new SkyDriveWebClient();
            //_client.LogOn("whsbackup@hotmail.com", "qazxsw");
            //_client.CreateRootWebFolder("WHSBackup", WebFolderItemShareType.Private);
            //WebDriveInfo _info = _client.GetWebDriveInfo();

            //WebFolderInfo[] _webFolderInfo = _client.ListRootWebFolders();

            //foreach (WebFolderInfo webFolderInfo in _webFolderInfo)
            //{
            //    if (webFolderInfo.Name == "WHSBackup")
            //    {
            //        _client.UploadWebFile(@"C:\Chrysler.txt", webFolderInfo);
            //    }
            //}
        }

        public bool GetHelp()
        {
            // Open a simple Message Box for Help
            //MsgBox("You clicked Help or pressed F1.", , "My Tab Help");

            // Open a custom compiled Help file (.chm)topic
            // this.services.OpenHelp(“MyHelpFile.chm”,”<MyTopicGUID>”);
            return true;
        }

        public string TabText
        {
            get
            {
                return "SkyDrive Backup";
            } 
        }

        public Bitmap TabImage
        {
            get
            {
                return Properties.Resources.SkyDrive;
            } 
        }

        public Control TabControl
        {
            get
            {
                return nPanel;
            }
        }

        public Guid SettingsGuid
        {
            get
            {
                return Guid.Empty;
            }
        }
    }
}
